package com.tadpole.music.utils;

import android.graphics.Bitmap;
import android.util.Log;
import android.widget.ImageView;

import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.BitmapCallback;
import com.zhy.http.okhttp.callback.Callback;
import com.zhy.http.okhttp.callback.FileCallBack;
import com.zhy.http.okhttp.callback.StringCallback;

import java.io.File;
import java.io.IOException;
import java.util.Map;
import java.util.concurrent.TimeUnit;

import okhttp3.Call;
import okhttp3.FormBody;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

/**
 * Created by Administrator on 2018/10/24 0024.
 */

public class HttpUtil {
    //可根据需求自定义Callback，根据parseNetworkResponse回调的response进行解析，运行在子线程
    //当Activity销毁时，根据添加的标签tag，在onDestory()内调用OkHttpUtils.getInstance().cancelTag(tag)停止网络请求
    //get请求
    public static void requestGet(String url, Callback callback, Object tag) {
        OkHttpUtils
                .get()
                .url(url)
                .tag(tag)
                .build()
                .execute(callback);
    }

    //post请求
    public static void requestPost(String url, Map<String, String> params, Callback callback, Object tag) {
        OkHttpClient client = new OkHttpClient.Builder()

                .connectTimeout(5, TimeUnit.SECONDS)//设置连接超时时间
                .readTimeout(5, TimeUnit.SECONDS)//设置读取超时时间
                .addInterceptor(new RetryIntercepter(0))
                .build();
        OkHttpUtils okHttp = new OkHttpUtils(client);

        okHttp.post()
                .url(url)
                .params(params)
                .tag(tag)
                .build()
                .execute(callback);
    }

    //post请求
    public static void requestPostHead(String url,String token, Map<String, String> params, Callback callback, Object tag) {
        OkHttpClient client = new OkHttpClient.Builder()

                .connectTimeout(5, TimeUnit.SECONDS)//设置连接超时时间
                .readTimeout(5, TimeUnit.SECONDS)//设置读取超时时间
                .addInterceptor(new RetryIntercepter(0))
                .build();
        OkHttpUtils okHttp = new OkHttpUtils(client);

        okHttp.post()
                .url(url)
                .params(params)
                .tag(tag)
                .addHeader("Authorization",token)
                .build()
                .execute(callback);
    }

    //put
    public static void requestPutHead(String url,String token,FormBody build, Callback callback, Object tag){
        OkHttpClient client = new OkHttpClient.Builder()

                .connectTimeout(5, TimeUnit.SECONDS)//设置连接超时时间
                .readTimeout(5, TimeUnit.SECONDS)//设置读取超时时间
                .addInterceptor(new RetryIntercepter(0))
                .build();
        OkHttpUtils okHttp = new OkHttpUtils(client);
        okHttp.put()
                .url(url)
                .requestBody(build)
                .tag(tag)
                .addHeader("Authorization",token)
                .build()
                .execute(callback);
    }



    //post请求
    public static void requestPostHeadNo(String url,String token, Callback callback, Object tag) {
        OkHttpClient client = new OkHttpClient.Builder()

                .connectTimeout(5, TimeUnit.SECONDS)//设置连接超时时间
                .readTimeout(5, TimeUnit.SECONDS)//设置读取超时时间
                .addInterceptor(new RetryIntercepter(0))
                .build();
        OkHttpUtils okHttp = new OkHttpUtils(client);

        okHttp.post()
                .url(url)
                .tag(tag)
                .addHeader("Authorization",token)
                .build()
                .execute(callback);
    }

    //get请求
    public static void requestGetHead(String url,String token, Map<String, String> params, Callback callback, Object tag) {
        OkHttpUtils
                .get()
                .url(url)
                .params(params)
                .tag(tag)
                .addHeader("Authorization",token)
                .build()
                .execute(callback);
    }




    public static void requestByPost(String url, Callback callback, Object tag) {
        OkHttpUtils.post().url(url).tag(tag).build().execute(callback);
    }

    //post Json
    public static void uploadJson(String url, String json) {
        OkHttpClient okHttpClient = new OkHttpClient.Builder()
                .connectTimeout(10, TimeUnit.SECONDS)
                .writeTimeout(10, TimeUnit.SECONDS)
                .readTimeout(20, TimeUnit.SECONDS)
                .build();

        //MediaType  设置Content-Type 标头中包含的媒体类型值
        RequestBody requestBody = FormBody.create(MediaType.parse("application/json; charset=utf-8")
                , json);   //json响应头
        final Request request = new Request.Builder()
                .url(url)//请求的url
                .post(requestBody)
                .build();

        //创建Call
        Call call = okHttpClient.newCall(request);
        //加入队列 异步操作
        call.enqueue(new okhttp3.Callback() {
            @Override
            public void onFailure(Call call, IOException e) {

            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {

            }
        });
    }

    //上传文件
    public static void uploadFile(String url, File file, StringCallback callback, Object tag) {
        //将文件作为请求体，发送到服务器。
        //进行文件上传下载需要进度条时 复写inProgress方法
        OkHttpUtils
                .postFile()
                .url(url)
                .file(file)
                .tag(tag)
                .build()
                .execute(callback);
    }

    //下载文件
    public static void downloadFile(String url, FileCallBack callback, Object tag) {
        //使用FileCallback,参数为路径名、文件名
        OkHttpUtils
                .get()
                .url(url)
                .tag(tag)
                .build()
                .execute(callback);
    }

    //显示图片下载
    public static void showNetPic(String url, final ImageView view, Object tag) {
        OkHttpUtils
                .get()
                .url(url)
                .tag(tag)
                .build()
                .execute(new BitmapCallback() {
                    @Override
                    public void onError(Call call, Exception e, int id) {
                        Log.i("errorMsg", e.toString());
                    }

                    @Override
                    public void onResponse(Bitmap response, int id) {
                        view.setImageBitmap(response);
                    }
                });
    }

    public static void requestNormal(String url, Object tag, Callback callback) {
        OkHttpClient client = new OkHttpClient.Builder()
                .connectTimeout(5, TimeUnit.SECONDS)//设置连接超时时间
                .readTimeout(5, TimeUnit.SECONDS)//设置读取超时时间
                .addInterceptor(new RetryIntercepter(0))
                .build();
        OkHttpUtils okHttp = new OkHttpUtils(client);
        okHttp.post()
                .url(url)
                .tag(tag)
                .build()
                .execute(callback);
    }

}
